package com.czk.netty.netty.tcp.protocaltcp.practice.server;

import com.czk.netty.netty.tcp.protocaltcp.practice.protocol.TransferProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/6/8 11:13
 */
public class MessageDecoder extends ReplayingDecoder<Void> {

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        System.out.println("解码器调用了");
        // 拿到长度
        int length = byteBuf.readInt();
        // 拿到数据
        byte[] buffer = new byte[length];
        // 读取数据
        byteBuf.readBytes(buffer);
        // 构建
        TransferProtocol transferProtocol = new TransferProtocol();
        transferProtocol.setLength(length);
        transferProtocol.setContent(buffer);
        // 给下一个handler
        list.add(transferProtocol);
    }
}
